<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/class_address.php');
include('class/class_workdays.php');
include('class/class_rating.php');
include('class/abstract/class_mainObjectInfo.php');
include('class/city_rus.php');
include('class/class_checkGetData.php');
include('includes/transliterate.php');

$aCheck=new checkGetData('m');
$aCheck->checkTableId($_GET['city'], $_GET['id']);
foreach($_GET as $key=>$vol)
	$_GET[$key]=$aCheck->safetyCheck($vol);
foreach($_POST as $key=>$vol)
{
	if($key=='firmToAdd')continue;
	$_POST[$key]=$aCheck->safetyCheck($vol);
}
session_start();
if(!$_SESSION['login'])
{
	header('location: registr.php?city='.$_GET['city']);
	exit;
}
$aConnect=new connect('m');
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
if(strtolower($_SESSION['login'])!=strtolower($row['user']))
	checkGetData::error();
if($_POST['deleteButton'])
{	if($_POST['firmToDelete'])
	{		$ma=explode(', ', $row['address']);
		$mallsStreet=$ma[0];
		$mallsBuilding=$ma[1];		$aConnect=new connect('f');
		$r=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_POST['firmToDelete']."'"));
		if($r)
		{			$newAddr=array();
			$newAddress=array();			$address=explode('|', $r['address']);
			if($address)
			{				foreach($address as $key=>$addr)
				{					$addrEls=explode('^', $addr);
					if($addrEls[0]==$mallsStreet && $addrEls[1]==$mallsBuilding
					&& $addrEls[3]==$row['id'].','.$row['name'].','.$row['short'])
					{
						$addrEls[3]='';
					}
					if(is_array($addrEls))$newAddr[]=implode('^', $addrEls);				}
				if(is_array($newAddr))$newAddress=implode('|', $newAddr);			}
			if($r['malls'])
			{				$malls=explode(',', $r['malls']);
				$key=array_search($_GET['id'], $malls);
				unset($malls[$key]);			}
			$newMalls=implode(',', $malls);
			$dataToUpdate="address='".$newAddress."', malls='".$newMalls."'";
			$aConnect->update_data($_GET['city'], $dataToUpdate, "WHERE id='".$r['id']."'");		}
		$aConnect=new connect('m');
		$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$firms=explode('|', $row['firms']);
		$key=array_search($r['id'].'^'.$r['name'].'^'.$r['subdiv'], $firms);
		unset($firms[$key]);
		$newFirms=implode('|', $firms);
		$aConnect->update_data($_GET['city'], "firms='".$newFirms."'", "WHERE id='".$_GET['id']."'");
		header('location: mallscabinet.php?city='.$_GET['city'].'&id='.$_GET['id']);
		exit;
	}}
class aFirmPage extends page
{
	var $row=array();
	var $cityRus;
	function __construct($row)
	{
		$this->row=$row;
		$this->cityRus=new city_rus_class();
		parent::__construct();
	}
	function title()
	{
		echo $this->row['name'].' '.$this->cityRus->city_rus_im($_GET['city']);
	}
	function maincontent()
	{
		echo ('
		<link rel="stylesheet" type="text/css" href="style/malls.css" />
		<link rel="stylesheet" type="text/css" href="style/addMallsFirms.css" />
		<script type="text/javascript" src="scripts/addmallsfirms.js">
		</script>
		<div class="photoFrame">
		</div>
		<script type="text/javascript" src="scripts/mallsSearchSuggest.js">
		</script>
		<div class="route">
			<div class="routeEl">
				<a href="pcabinet.php?city='.$_GET['city'].'">Личный кабинет</a>
			</div>
			<div class="routeEl">
				<a href="my_malls.php?city='.$_GET['city'].'">Мои центры</a>
			</div>
			<div class="routeEl">
				<a href="mallscabinet.php?city='.$_GET['city'].'&id='.$_GET['id'].'">'.$this->row['name'].'</a>
			</div>
			<div class="routeEl">
				Фирмы
			</div>
		</div>
		<div id="formInfo">
			Здесь вы можете удалить фирмы, добавленные в ваш торговый центр по ошибке<br />
		</div>
		');

		$aConnect=new connect('m');
		$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$mallsFirmsArray=explode('|', $row['firms']);
		for($i=0; $i<count($mallsFirmsArray); $i++)
		{
			$aMallsFirm=$mallsFirmsArray[$i];
			$aMallsFirmEl=explode('^', $aMallsFirm);
			$aMallsFirmId=$aMallsFirmEl[0];
			$aMallsFirmName=$aMallsFirmEl[1];
			$aMallsFirmSubdiv=$aMallsFirmEl[2];
			$aFirm[$aMallsFirmSubdiv][]=$aMallsFirmId.','.$aMallsFirmName;

		}
		arsort($aFirm);
		echo ('
		<center>
			<h3 id="formHeader">Фирмы - '.$this->row['name'].'</h3>
		</center>
		<form id="deleteMallsFirms" name="deleteMallsFirms" method="post">
		');
		foreach($aFirm as $Firm=>$subdiv)
		{
			echo ('
			<div class="mallsSubdiv" align="center">
				<a href="">'.$Firm.' ('.count($subdiv).')</a>
			</div>
			<div class="mallsFirmsList">
			');
			for($i=0; $i<count($aFirm); $i++)
			{
				$aFirmsId=$subdiv[$i];
				$firmsIdNameArray=explode(',', $subdiv[$i]);
				$aFirmsId=$firmsIdNameArray[0];
				$aFirmsName=$firmsIdNameArray[1];
				echo ('
						<div class="mallsFirms" id="mallsFirms'.$aFirmsId.'">
						');
				if(file_exists('pictures/firmspics/'.$_GET['city'].'/'.$aFirmsId.'.jpg'))
				{
					echo ('
							<img class="mallsPics" src="pictures/firmspics/'.$_GET['city'].'/'.$aFirmsId.'.jpg" title="'.$aFirmsName.'" />
						');
				}
				echo ('
						</div>
					');
			}
			echo ('
			</div>
			');
	  	}
		echo ('
		</form>
		<link rel="stylesheet" type="text/css" href="style/cabinet.css" />
		<div class="cab-panel">
			<div class="cab-panel-link">
				<a class="belink" href="addmallsfirms.php?city='.$_GET['city'].'&id='.$_GET['id'].'">Добавить фирмы</a>
			</div>
		</div>
		');
	}
}


$aPage=new aFirmPage($row);

?>